package www.dashu.com.controller;

import www.dashu.com.api.CommonResult;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.SysApp;
import www.dashu.com.dto.AppQuery;
import www.dashu.com.service.SysAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@Api(tags = "SysAppController", description = "应用管理")
@RequestMapping("/app")
public class SysAppController
{

    @Autowired
    SysAppService appService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "应用列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<SysApp>> list(AppQuery query) {

        try {
            Page<SysApp> pageData = appService.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增应用")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody SysApp app) {
        app.setId(appService.createId(app));
        app.setCreateTime(new Date());
        app.setUpdateTime(new Date());
        appService.save(app);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改应用")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody SysApp app) {
        app.setUpdateTime(new Date());
        appService.save(app);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除应用")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody SysApp app) {
        appService.deleteById(app.getId());
        return CommonResult.success();
    }

}
